<?php

namespace App\Repository\Order;

use App\Models\Order\OrderLogModel as model;
use App\Repository\BaseRepository;
use App\Repository\Order\OrderInfoRepository;

class OrderLogRepository extends  BaseRepository
{
  public function __construct()
  {
    $this->model = new model();
    $this->modelInfo = new OrderInfoRepository();
    // $this->productModel = $ProductRepository;
    parent::__construct();
  }
  //添加
  public function add($data)
  {
    //必填 字段
    $mustField = [
      'orderno',
    ];
    $optionalFields = $this->model->getTableField();
    $msgField = [
      'orderno' => '订单号不能为空',
    ];
    $insertData  = $this->getRealData($data, $mustField, $optionalFields, $msgField);
    if (!empty($insertData) && is_string($insertData)) {
      //返回错误参数
      return $this->returnMsg(
        2,
        $insertData
      );
    }
    $isTrue = $this->model->where(['orderno' => $insertData['orderno']])->first();
    if ($isTrue) {
      $insertData['remark'] = $isTrue->remark . '<br/>' . $insertData['remark'];
      $res = $this->model->where(['orderno' => $insertData['orderno']])->update($insertData);
    } else {
      $insertData['created_at'] = date('Y-m-d H:i:s');
      $res = $this->model->insert($insertData);
    }
    if ($res) {
      return $this->returnMsg(200, '上传成功');
    }
    return $this->returnMsg(1, '上传失败');
  }
  //删除
  public function del($where)
  {
    $flag = true;
    $msg = '未知错误';
    $code = 1;
    if (empty($where)) {
      $flag = false;
      $msg = '删除条件不能为空';
      $code = 2;
    }
    if ($flag) {
      $res = $this->model->where($where)->get();
      if (!$res) {
        $flag = false;
        $msg = '未查询到数据';
        $code = 3;
      }
    }
    if ($flag) {
      $del = $this->model->where($where)->delete();
      if ($del) {
        $msg = '删除成功';
        $code = 200;
      }
    }
    return $this->returnMsg($code, $msg);
  }
  //修改
  public function edit($where, $data, $isMust = false)
  {
    if (empty($where)) {
      return $this->returnMsg(1, '条件不能为空');
    }
    if (empty($data)) {
      return $this->returnMsg(2, '修改参数不能为空');
    }
    //过滤字段
    //过滤字段
    $optionalFields = $this->model->getTableField();
    $field  = $this->getRealData($data, [], $optionalFields, []);

    //先查询是否存在
    $res = $this->model->where($where)->update($field);
    if ($isMust && $res) {
      return $this->returnMsg(200, '修改成功');
    }
    if (!$isMust) {
      return $this->returnMsg(200, '修改成功');
    }
    return $this->returnMsg(3, '请重新修改');
  }
  //列表查询
  public function lists($where = [], $page = 1, $limit = 10)
  {
    $res = $this->search($this->model, $where, $page, $limit);

    return $res;
  }
  public function getOne($where, $field = ['*'])
  {
    return $this->model->where($where)->select($field)->first();
  }
  //下单
  public function orderSubmit()
  {
  }
  // 处理订单的状态 
  // 订单的状态 1: 返回的物流等信息 修改 2:
  public function editOrderStatus($data)
  {
    if (empty($data)) {
      return $this->returnMsg(1, '修改的数据为空');
    }
    if (empty($data['orderno'])) {
      return $this->returnMsg(2, '订单号不能为空');
    }
    $update = [];
    if (!empty($data['express_company']) && !empty($data['status']) && $data['status'] < 20) {
      $data['status'] = 20; // 已发货的订单
    }
    if (!empty($data['status'])) {
      $update['status'] = $data['status'];
      $update['status_desc'] = $this->model->getOrderStatusDesc($data['status']);
    }

    $where = [];
    if (!empty($data['orderno'])) {
      $where = ['orderno' => $data['orderno']];
      unset($data['orderno']);
    }
    // 查询订单 是否存在
    $isExist = $this->model->where($where)->select(['id'])->orderBy('id', 'desc')->first();
    if (!$isExist) {
      return $this->returnMsg(3, '未查询到订单');
    }
    $isTrue = $this->model->where(['id' => $isExist->id])->update($update);
    if ($isTrue) {
      $this->modelInfo->edit($update);
    }
    return $isTrue;
  }
}
